package com.sihan.swvp.action.wg;

import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sihan.swvp.action.BaseAction;
import com.sihan.swvp.entity.wg.WgDxwg;
import com.sihan.swvp.service.sys.LogService;
import com.sihan.swvp.service.wg.WgDxwgService;

@Controller
@RequestMapping("/wg/WgDxwgAction")
public class WgDxwgAction extends BaseAction{

	@Autowired
	private WgDxwgService wgDxwgService;
	@Autowired
	public LogService logService;
	
	@ResponseBody
	@RequestMapping("/getWgDxwg")
	public Object getWgDxwg(@Param("dxwgName")String dxwgName,@Param("page")Integer page,@Param("limit")Integer limit){
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("dxwgName", dxwgName);
		return pageData(wgDxwgService.getWgDxwg(params, page, limit));
	}
	
	@ResponseBody
	@RequestMapping("/addWgDxwg")
	public Object addWgDxwg(WgDxwg bean){
		try {
			Map<String, Object> validate=wgDxwgService.getWgDxwgValidate(bean);
			int nameNubmer=Integer.parseInt(validate.get("nameNubmer").toString());
			if(nameNubmer !=0){
				return failMsg("该典型违规名称已存在");
			}	
			Long userId=getLoginUser().getId();
			bean.setUpuser(userId);
			wgDxwgService.addWgDxwg(bean);
			logService.addUserLog("添加典型违规", "典型违规", getLoginUser().getId(), "添加典型违规："+bean.getDxwgName(),getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("添加典型违规", "典型违规", getLoginUser().getId(), "添加典型违规："+bean.getDxwgName(),getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
	
	@ResponseBody
	@RequestMapping("/updateWgDxwg")
	public Object updateWgDxwg(WgDxwg bean){
		try {
			Map<String, Object> validate=wgDxwgService.getWgDxwgValidate(bean);
			int nameNubmer=Integer.parseInt(validate.get("nameNubmer").toString());
			if(nameNubmer !=0){
				return failMsg("该典型违规名称已存在");
			}	
			bean.setUpuser(getLoginUser().getId());
			wgDxwgService.updateWgDxwg(bean);
			logService.addUserLog("修改典型违规", "典型违规", getLoginUser().getId(), "修改典型违规："+bean.getDxwgName(),getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("修改典型违规", "典型违规", getLoginUser().getId(), "修改典型违规："+bean.getDxwgName(),getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
	
	@ResponseBody
	@RequestMapping("/deleteWgDxwg")
	public Object deleteWgDxwg(@Param("id")Integer id,String dxwgName){
		try {
			wgDxwgService.deleteWgDxwg(id);
			logService.addUserLog("删除典型违规", "典型违规", getLoginUser().getId(), "删除典型违规："+dxwgName,getRemoteAddr(),1,0);
			return successMsg("");
		} catch (Exception e) {
			e.printStackTrace();
			logService.addUserLog("删除典型违规", "典型违规", getLoginUser().getId(), "删除典型违规："+dxwgName,getRemoteAddr(),0,0);
			return failMsg("服务器异常");
		}
	}
}
